package render

import (
	"net/http"

	"github.com/go-chi/render"
)

type AppBaseResponse struct {
	Err            error `json:"-"` // low-level runtime error
	HTTPStatusCode int   `json:"-"` // http response status code

	StatusText string `json:"status"`          // user-level status message
	AppCode    int64  `json:"code,omitempty"`  // application-specific error code
	ErrorText  string `json:"error,omitempty"` // application-level error message, for debugging

	Data any `json:"data,omitempty"`
}

type ListData[T any] struct {
	Rows []T `json:"rows"`
	Total int64 `json:"total"`    
}

func (e *AppBaseResponse) Render(w http.ResponseWriter, r *http.Request) error {
	render.Status(r, e.HTTPStatusCode)
	return nil
}

func DefaultToRender() render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok"}
}

func TypedToRender[T any](v T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: v}
}
